package com.example.service;

import com.alibaba.fastjson.JSONObject;
import com.example.mapper.OrderMapper;
import com.example.mapper.ProductMapper;
import com.example.mapper.ShoppingCarMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Order;
import com.example.pojo.ShoppingCar;
import com.example.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.sql.Date;
import java.sql.Time;
import java.util.List;
import java.util.Timer;

/**
 * @Description:
 * @author: GaoPeng
 * @date: 2021/5/26 21:40
 */
@RequestMapping("/orderService")
@RestController
@Service
@CrossOrigin
public class OrderService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ObjectMapper jsonMapper;

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private ProductMapper productMapper;


    /**
     * 收银状态
     * @param username 手机号
     * @param receiver 收件人
     * @param address 邮寄地址
     * @param post_code 邮政编码
     * @param tel 联系电话
     * @param pay_way 付款方式
     * @param transfer_way 运输方式
     * @param remark 备注
     * @return
     */
    @PostMapping("/pay")
    Boolean pay(@PathParam("username")String username,@PathParam("receiver")String receiver,@PathParam("address")String address,@PathParam("post_code")String post_code,@PathParam("tel")String tel
            ,@PathParam("pay_way")String pay_way,@PathParam("transfer_way")String transfer_way,@PathParam("remark")String remark){

        int cnt=0;
        List<ShoppingCar> shoppingCarList = shoppingCarMapper.selectAllByUser_id(userMapper.selectIdByUsername(username));
        System.out.println("*********************************");
        System.out.println(shoppingCarList);
        System.out.println("*********************************");
        for (ShoppingCar shoppingCar : shoppingCarList) {
            Order order=new Order();

            order.setProduct_id(shoppingCar.getProduct_id());
            order.setSeller_id(productMapper.selectSellerById(shoppingCar.getProduct_id()));
            System.out.println("9999999999999999999"+productMapper.selectSellerById(shoppingCar.getProduct_id()));
            order.setAmount(shoppingCar.getAmount());
            order.setUser_id(shoppingCar.getUser_id());
            order.setCreate_time(new Date(System.currentTimeMillis())+" "+new Time(System.currentTimeMillis()));
            order.setReceiver(receiver);
            order.setAddress(address);
            order.setPost_code(post_code);
            order.setTel(tel);
            order.setPay_way(pay_way);
            System.out.println(pay_way);
            order.setTransfer_way(transfer_way);
            order.setRemark(remark);
            System.out.println("000000000000000000000000");
            System.out.println(order);
            orderMapper.insertOrder(order);
            cnt++;
        }



        return cnt==shoppingCarList.size();

    }

    /**
     * 通过username获取所有的订单
     * @param username 手机号码
     * @return
     */
    @GetMapping("/getAllOrdersByUsername/{username}")
    String getAllOrdersByUsername(@PathVariable String username){
        Integer user_id = userMapper.selectIdByUsername(username);
        List<Order> orderList = orderMapper.selectAllByUser_id(user_id);
        try {
            return jsonMapper.writeValueAsString(orderList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return "something wrong";
    }

    /**
     * 通过订单号获取单个订单信息
     * @param id
     * @return
     */
    @GetMapping("/getOrderById/{id}")
    String getOrderById(@PathVariable Integer id){
        try {
            return jsonMapper.writeValueAsString(orderMapper.selectById(id));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return "something wrong";
    }

    /**
     * 保存退货信息
     * @param order_id 订单号
     * @param reason 退货原因
     * @return
     */
    @PostMapping("/tuihuo")
    Boolean tuihuo(@RequestParam("order_id")Integer order_id,@RequestParam("reason") String reason){
        return orderMapper.tuihuo("退货："+reason,order_id)>0;
    }

    /**
     * 退货：用户发起退货，将状态改为1
     * 管理员同意退货，将状态改为2
     * 不同意将状态改为3
     * @param order_id
     * @param status
     * @return
     */
    @PostMapping("/changeStatus")
    Boolean changeStatus(@RequestParam("order_id")Integer order_id,@RequestParam("status") Integer status){
        return orderMapper.changeStatus(order_id,status)>0;
    }

    /**
     * 通过订单号查订单状态
     * @param order_id
     * @return
     */
    @GetMapping("/getStatus/{order_id}")
    Integer getStatus(@PathVariable Integer order_id){
        return orderMapper.selectStatus(order_id);
    }

    @GetMapping("/getAllOrders")
    String getAll(){
        try {
            return jsonMapper.writeValueAsString(orderMapper.selectAll());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return null;
    }



}
